app.controller('MapStationCtrl', function ($scope, ajax, $modal, toaster) {

    /* global BMap */
    $scope.ad = "";
    $scope.point = "";
    $scope.addressMore = "";

    $scope.$on('han.map.init', function () {
        var map = $scope.map;
        var local = new BMap.LocalSearch(map, { onSearchComplete: myFun });
        var ac = new BMap.Autocomplete({ "input": "suggestId", "location": map });
        ac.addEventListener("onhighlight", function (e) {  //鼠标放在下拉列表上的事件
            var str = "";
            var _value = e.fromitem.value;
            var value = "";
            if (e.fromitem.index > -1) {
                value = _value.province + _value.city + _value.district + _value.street + _value.business;
            }
            str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

            value = "";
            if (e.toitem.index > -1) {
                _value = e.toitem.value;
                value = _value.province + _value.city + _value.district + _value.street + _value.business;
            }
            str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
            G("searchResultPanel").innerHTML = str;
        });

        var myValue;
        ac.addEventListener("onconfirm", function (e) {    //鼠标点击下拉列表后的事件
            var _value = e.item.value;
            myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
            G("searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

            $scope.address = myValue;
            local.search(myValue);
        });

        function G(id) { return document.getElementById(id); }
        function myFun() {
            var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
            var am = local.getResults().getPoi(0).address;   //获取详细地址
            var ad = local.getResults().getPoi(0).title;   //获取详细地址
            $scope.point = pp;
            $scope.ad = ad;
            $scope.addressMore = am;
            map.clearOverlays();    //清除地图上所有覆盖物
            map.centerAndZoom(pp, 15);
            map.addOverlay(new BMap.Marker(pp));    //添加标注
        }
    });

    $scope.add = function () {
        if ($scope.point == "") {
            toaster.pop('warning', '提示', '请输入正确地址！');
            return;
        }
        $modal.open({
            templateUrl: 'template/bus/station/writeModal.html?v=22e8ac2949',
            controller: 'BusStationModalCtrl',
            size: 'lg',
            resolve: {
                param: function () {
                    return {
                        "point": $scope.point,
                        "address": $scope.address,
                        "addressMore": $scope.addressMore,
                        "ad": $scope.ad
                    };
                }
            }
        }).result.then(function (param) {
            ajax.post('station/bs/insert', param, function (result) {
                if (result && result.code == 200) {
                    toaster.pop('success', '成功', '新增信息成功。');
                    $scope.point = "";
                } else {
                    toaster.pop('error', '错误', result.result);
                }
            }, function (result) {
                toaster.pop('error', '错误', result.result);
            });
        });
    };
});
